package a51qindian.com.comprehensive.util;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.PopAdapter;
import a51qindian.com.comprehensive.model.AddressInfo;

/**
 * Author: Li
 * Date: 2017/11/2 0002
 * Description: 筛选条件
 */

public class PopCondition extends FrameLayout {
    private RotateAnimation mAnimationDismiss;
    private RotateAnimation mAnimationShow;
    private Context mContext;
    private View mView;
    private PopAdapter mPopAdapter;

    public PopCondition(@NonNull Context context) {
        super(context);
    }

    public PopCondition(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init(context,attrs);
    }

    private TextView mTxtPop;
    private ImageView mImgPop;
    private PopupWindow mPopupWindow;
    private String mFirstContent;
    private String mFirstId;

    public void setOnBack(OnBack onBack) {
        mOnBack = onBack;
    }

    private OnBack mOnBack;
    private void init(Context context, AttributeSet attrs) {
        mView = LayoutInflater.from(context).inflate(R.layout.layout_pop,this);
        mTxtPop= mView.findViewById(R.id.txt_pop);
        mImgPop= mView.findViewById(R.id.img_pop);
        mAnimationShow = new RotateAnimation(0,180, Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        mAnimationShow.setDuration(300);
        mAnimationShow.setFillAfter(true);
        mAnimationDismiss = new RotateAnimation(-180,0, Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        mAnimationDismiss.setDuration(300);
        mAnimationDismiss.setFillAfter(true);



        View content=LayoutInflater.from(mContext).inflate(R.layout.view_layout_pop,null);
        RecyclerView recyclerView=content.findViewById(R.id.recycler_content);
//        mPopupWindow=new PopupWindow(content,-1, (int) (ScreenUtil.getScreenHeight(context)*0.4));
        mPopupWindow=new PopupWindow(content,-1, -1);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mPopAdapter = new PopAdapter(mContext);
        mPopAdapter.setItemClickListener(new PopAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, String id,String text) {
                mFirstContent=text;
                mFirstId=id;
                setPopText(mFirstContent);
                mPopupWindow.dismiss();
                if (mOnBack != null) {
                    mOnBack.onBack();
                }
            }
        });
        recyclerView.setAdapter(mPopAdapter);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mImgPop.startAnimation(mAnimationDismiss);
                mView.setSelected(false);
            }
        });
        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });


    }

    public boolean isShowing(){
        return mPopupWindow.isShowing();
    }
    public void dismiss(){
        if (mPopupWindow == null) {
            return;
        }
        mPopupWindow.dismiss();
    }
    public void setData(List<AddressInfo> addressInfoList){
        mFirstId=addressInfoList.get(0).getUid();
        mFirstContent=addressInfoList.get(0).getName();
        setPopText(mFirstContent);
        mPopAdapter.setAddressInfoList(addressInfoList);
    }
    public void showPop(View view){
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }else {
            mView.setSelected(true);
            int[] location=new int[2];
            view.getLocationInWindow(location);
            view.measure(0,0);
            int height=view.getMeasuredHeight();
            mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY,0,location[1]+height);
            mImgPop.startAnimation(mAnimationShow);
        }
    }
    public PopupWindow getPop(){
        return mPopupWindow;
    }


    public void setPopText(String txt){
        mTxtPop.setText(txt);
    }

    public String getUId(){

        return mFirstId==null?"":mFirstId;
    }
    public String getName(){
        return mFirstContent;
    }

    public interface OnBack{
        void onBack();
    }
}
